<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\blog\model;
use think\Model;
use think\Db;
class MenuModel extends Model
{
    /*
     * 类别查询
     * */
    public function index($where = '')
    {

        $data = DB::name('goods_classify')->where($where)->order('id desc')->select();
        return $data;
    }

    /*
     * 查询商品
     * 更新商品的查询 以每个店为选择单位
     * array_filter 删除数组中空值
     * */
    public function goods($data = '', $category = '')
    {
        $where['id'] = $data;
        $store = DB::name('store')->where($where)->find();
        $store_ids = $store['cat_ware'];
        $goods = array_filter(explode(',', $store_ids));
        $array = array();
        foreach ($goods as $k => $v) {
            $cause['g_id'] = $v;
            if (!empty($category['id'])) {
                $cause['g_category'] = $category['id'];
            }
            $state=DB::name('goods')->where($cause)->find();
            if($state)
            {
                $array['data'][] =$state;
            }

        }
        $array['store_type']=$store['cat_store'];
        return $array;
    }
    /*
     * 套餐查询
     * */
    public function set($where='')
    {
        $data=DB::name('setmeal')->where($where)->order('set_id desc')->select()->toArray();
        foreach($data as $k=>$v)
        {
            $data[$k]['g_id']=$v['set_id'];
            $data[$k]['g_price']=$v['set_price'];
            $data[$k]['g_goodsname']=$v['set_name'];
            $data[$k]['format']="无";
            $data[$k]['stemeal']=1;
        }
        return $data;
    }
    /*
         * 展示商品  选择菜单，挂单选择  套餐选择等
         * */
    public function show($where)
    {
        $data=DB::name('order_detail')->where($where)->select()->toArray();
        foreach($data as $k=>$v)
        {
            $formatid=$v['format'];
            $cause['id']=$formatid;
            $format=DB::name('format')->where($cause)->find();
            $data[$k]['formatname']=$format['format'];
        }
        return $data;
    }
    /*
     * 单个商品的查询
     * */
    public function good($id = '', $format = '')
    {
        $where['g.g_id'] = $id;
        $where['f.id'] = $format;
        $array = DB::name('goods')->alias('g')->join('format f', 'f.pid = g.g_id')->where($where)->find();
        return $array;
    }

    /*
     *查询规格
     * */
    public function format($where = "")
    {
        $data = DB::name('format')->where($where)->select()->toArray();
        return $data;
    }

    /*
     * 求总额
     * */
    public function setpay($session)
    {
        $data = array();
        $data['all_money'] = 0;
        $data['num'] = count($session);
        foreach ($session as $k => $v) {
            $data['all_money'] = $data['all_money'] + $v['price'];
        }
        return $data;
    }

    /*
    * 求折扣总和 和需要支付的总额
    * c_rmbs 折扣优惠总额
    * all_money  需要付款总额
    * s_pay 实际付款
    *套餐不牵扯到打折
    * */
    public function setpays($session)
    {
        $data = array();
        $data['c_rmbs'] = 0;
        $data['all_money'] = 0;
        foreach ($session as $k => $v) {

            $data['all_money'] = $data['all_money'] + $v['g_price'];
            $data['c_rmbs'] = $data['c_rmbs'] + $v['c_rmb'];

        }
        $data['s_pay'] = $data['all_money'] - $data['c_rmbs'];
        return $data;
    }

    /*
     * 查询折扣
     * */
    public function promotion($where = "")
    {
        $data = DB::name('promotion')->where($where)->select();
        return $data;
    }

    /*
     * 从数据库中查询
     * */
    public function selorder($order = '')
    {
        $where['orderid'] = $order;
        $data = DB::name('order_detail')->where($where)->order('id desc')->select();
        return $data;
    }
    /*
     *查询总额
     * */
    public function selorders($order = '')
    {
        $where['orderNO'] = $order;
        $data = DB::name('order')->where($where)->order('id desc')->find();
        return $data;
    }

    /*
     * 订单存储
     *  order  订单表
     *  order_detail  订单详情表
     * */
    public function saveorder($v, $shopid, $get_orderid)
    {
        $order = $this->saves($v, $shopid, $get_orderid);
        $runt = rand(1000,9999);
        $data['id'] =time().$runt;//2018/05/30 name 乾坤
        $data['orderid'] = $order['orderNo'];
        if($v['setmeal']==0)
        {
            $data['format'] = $v['id'];
            $data['price'] = $v['price'];
            $data['g_goodsname'] = $v['g_goodsname'];
            $data['goodsid'] = $v['g_id'];
        }else{
            $data['format'] = 0;
            $data['price'] = $v['set_price'];
            $data['g_goodsname'] = $v['set_name'];
            $data['goodsid'] = $v['set_id'];
        }
        $data['money'] = 0;
        $data['rmb'] = 0;
        $data['rebateid'] = 0;
        $data['vipprice'] = 0;
        $data['storeid'] = $shopid;
        $data['ctime'] = $order['ctime'];
        $data['paystatus'] = 0;
        if($v['setmeal'] == 0) {
            $data['stemeal'] = 0;
        } else {
            $data['stemeal'] = 1;
        }
        DB::name('order_detail')->insert($data);
        return $order['orderNo'];
    }

    /*
     * 订单order表
     * **/
    public function saves($order, $hopid, $get_orderid)
    {
        if ($get_orderid != 0) {
            $where['orderNo'] = $get_orderid;
            $old = DB::name('order')->where($where)->find();
            if($order['setmeal']==0)
            {
                $new_money = $old['totalMoney'] + $order['price'];
            }else{
                $new_money = $old['totalMoney'] + $order['set_price'];
            }

            $date['totalMoney'] = $new_money;
            DB::name('order')->where($where)->update($date);
            $data['orderNo'] = $get_orderid;
            $data['ctime'] = $old['ctime'];
        } else {
            $rand = rand(1000,9999);
            $data['id'] = time().$rand;//2018/05/30 name 乾坤
            $time = date('YmdHis', time());
            $data = array();
            $data['orderNo'] = $hopid . $time;
            $data['operatorid'] = 1;
            $data['storeid'] = $hopid;
            if($order['setmeal']==0)
            {
                $data['totalMoney'] = $order['price'];
            }else{
                $data['totalMoney'] = $order['set_price'];
            }
            $data['actualMoney'] = 0;
            $data['discount'] = 0;
            $data['getMoney'] = 0;
            $data['giveChange'] = 0;
            $data['payType'] = 0;
            $data['payStatus'] = 0;
            $data['takeOut'] = 0;
            $data['userName'] = 0;
            $data['userMobile'] = 0;
            $data['userAddress'] = 0;
            $data['ctime'] = time();
            $data['utime'] = 0;

            $state = DB::name('order')->insert($data);
        }
        return $data;
    }
    /*
     * 挂单状态
     * */
    public function updata($data='',$where='')
    {
        $state=DB::name('order')->where($where)->update($data);
        $data=DB::name('order')->where(['payStatus'=>-1])->select();
        return $data;
    }
    /*
     * 商品删除操作
     * */
    public function del($orderid,$id)
    {
        $where['id']=$id;
        $where['orderid']=$orderid;
        $state=DB::name('order_detail')->where($where)->find();
        if($state)
        {
            $cause['orderNo']=$orderid;
            $old=DB::name('order')->where($cause)->field('totalMoney')->find();
            $new=$old['totalMoney']-$state['price'];
            $array['totalMoney']=$new;
            $s=DB::name('order')->where($cause)->update($array);
            DB::name('order_detail')->where($where)->delete();
        }
        $data=DB::name('order_detail')->where(['orderid'=>$orderid])->select()->toArray();
        foreach($data as $k=>$v)
        {
            $why['id']=$v['format'];
            $format=DB::name('format')->where($why)->find();
            $data[$k]['formatname']=$format['format'];
        }
        if($data)
        {
            $array=$data;
        }else{
            $array=0;
        }
        return $data;
    }

}